<?php

namespace App\Repository;

use App\Models\AuthWechat;
use App\Models\AuthWx;
use App\Models\User;

class AuthWxRepository extends BaseRepository
{
    /**
     * 新增授权信息
     * @param $params
     * @return AuthWx|\Illuminate\Database\Eloquent\Builder|AuthWx
     */
    public function create($params)
    {
        return AuthWx::getModel()->create($params);
    }

    /**
     * @param AuthWechat $auth
     * @return mixed
     */
    public function delAuth($auth)
    {
        return $auth->delete();
    }

    /**
     * 通过用户ID，获取授权信息
     * @param string $userId
     * @param array $selects
     * @return AuthWx|\Illuminate\Database\Eloquent\Builder|object|null
     */
    public function getAuthInfoByUserId($userId, $selects = ['*'])
    {
        return AuthWx::getModel()
            ->select($selects)
            ->where('user_id', $userId)
            ->first();
    }

    /**
     * 通过unionid获取用户
     * @param $unionid
     * @return User|false|\Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed
     */
    public function getUserInfoByUnionid($unionid)
    {
        $auth = AuthWx::getModel()
            ->where('unionid', $unionid)
            ->first();
        if (!$auth || empty($auth->user)) {
            return false;
        }

        return $auth->user;
    }
}
